package com.ivi.car.module.home.model;

import android.arch.lifecycle.Lifecycle;

import com.ivi.car.base.BaseModel;
import com.ivi.car.bean.home.CarMaintainInfoBean;
import com.ivi.car.bean.home.CarStatusInfoBean;
import com.ivi.car.bean.home.HomeAdvBean;
import com.trello.rxlifecycle2.LifecycleProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import io.reactivex.Observable;

/**
 * Copyright
 * author zhengcheng
 * date 2018/3/7
 * describe
 * history
 */

public class HomeModel extends BaseModel implements IHomeContract.Model {
  private static final int PROBLEMS_COUNT = 4;
  private static final int ADV_COUNT = 5;
  private static final int RANDOM_STATUS = 3;
  public static final int STATUS_NORMAL = 0;
  public static final int STATUS_MAINTAIN = 1;
  public static final int STATUS_FAULT = 2;
  List<CarStatusInfoBean.CarInfo> list;
  public HomeModel(LifecycleProvider<Lifecycle.Event> provider) {
    super(provider);
  }

  @Override
  public Observable<CarStatusInfoBean> loadCarStatusInfo() {
    CarStatusInfoBean carStatusInfoBean = new CarStatusInfoBean();
    if (list != null && list.size() > 0) {
      list.clear();
    }
    int random = new Random().nextInt(RANDOM_STATUS);
    switch (random) {
      case STATUS_NORMAL:
        carStatusInfoBean.setStatus(STATUS_NORMAL);
        carStatusInfoBean.setStatusTitle("距离下次保养");
        list = new ArrayList<>();
        CarStatusInfoBean.CarInfo carInfoNormal = new CarStatusInfoBean.CarInfo();
        carInfoNormal.setInfoDesc("7500公里");
        list.add(carInfoNormal);
        CarStatusInfoBean.CarInfo carInfoNormal1 = new CarStatusInfoBean.CarInfo();
        carInfoNormal1.setInfoDesc("255天");
        list.add(carInfoNormal1);
        carStatusInfoBean.setCarInfos(list);
        break;
      case STATUS_MAINTAIN:
        carStatusInfoBean.setStatus(STATUS_MAINTAIN);
        carStatusInfoBean.setStatusTitle("有四个零配件需要保养");
        list = new ArrayList<>();
        for (int i = 0; i < PROBLEMS_COUNT; i++) {
          CarStatusInfoBean.CarInfo carInfo = new CarStatusInfoBean.CarInfo();
          carInfo.setInfoDesc("机油滤清器");
          list.add(carInfo);
        }
        carStatusInfoBean.setCarInfos(list);
        break;
      case STATUS_FAULT:
          carStatusInfoBean.setStatus(STATUS_FAULT);
          carStatusInfoBean.setStatusTitle("有4個故障，建议维修");
          list = new ArrayList<>();
          for (int i = 0; i < PROBLEMS_COUNT; i++) {
            CarStatusInfoBean.CarInfo carInfo = new CarStatusInfoBean.CarInfo();
            carInfo.setInfoDesc("故障描述" + (i + 1));
            list.add(carInfo);
          }
        break;
       default:

         break;
    }
    carStatusInfoBean.setCarInfos(list);
    return Observable.just(carStatusInfoBean)
        .compose(mProvider.<CarStatusInfoBean>bindToLifecycle());
  }

  @Override
  public Observable<List<CarMaintainInfoBean>> loadMaintainInfo() {
    List<CarMaintainInfoBean> list = new ArrayList<>();
    CarMaintainInfoBean maintainInfoOne = new CarMaintainInfoBean("预约保养", "支持带取送车服务");
    CarMaintainInfoBean maintainInfoTwo = new CarMaintainInfoBean("美容", "镀膜镀金，车内清洁");
    CarMaintainInfoBean maintainInfoThr = new CarMaintainInfoBean("洗车", "赠送免费检测服务");
    CarMaintainInfoBean maintainInfoFour = new CarMaintainInfoBean("车辆维修", "4S点专业技术");
    list.add(maintainInfoOne);
    list.add(maintainInfoTwo);
    list.add(maintainInfoThr);
    list.add(maintainInfoFour);
    return Observable.just(list).compose(mProvider.<List<CarMaintainInfoBean>>bindToLifecycle());
  }

  @Override
  public Observable<List<HomeAdvBean>> loadHomeAdv() {
    List<HomeAdvBean> list = new ArrayList<>();
    for (int i = 0; i < ADV_COUNT; i++) {
      HomeAdvBean advBean = new HomeAdvBean("威马4S店保养体验", "2017.09.30-2018.09.30", "800");
      list.add(advBean);
    }
    return Observable.just(list).compose(mProvider.<List<HomeAdvBean>>bindToLifecycle());
  }
}
